package com.woniu.hospital.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.hospital.domain.Discharge;
import com.woniu.hospital.service.IDischargeService;
import com.woniu.hospital.service.IHosptilpriceTypeService;
import com.woniu.hospital.service.IPatientService;
import com.woniu.hospital.vo.DischargeVo;
import com.woniu.user.domain.Patient;
import com.woniu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 出院费用总计Controller
 * 
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/discharge")
public class DischargeController extends BaseController
{
    @Autowired
    private IDischargeService dischargeService;
    @Autowired
    private IHosptilpriceTypeService hosptilpriceTypeService;
    @Autowired
    private IPatientService iPatientService;
    /**
     * 查询出院费用总计列表
     */
    /*@RequiresPermissions("hospital:discharge:list")*/
    @GetMapping("/list")
    public TableDataInfo list(Discharge discharge) {
        if(discharge.getDischargeStatus() != null){
            System.out.println("身份证============");
            Patient patient = iPatientService.selByIdCard(discharge.getDischargeStatus());
            discharge.setDischargeStatus(null);
            if(patient == null){
                return getDataTable(new ArrayList<>());
            }
            discharge.setPatientId(patient.getPatientId());
            System.out.println(discharge);
        }
        if(discharge.getHosptilpriceTypeid() != null){
            System.out.println("缴费状态============"+discharge.getHosptilpriceTypeid());
            if(discharge.getHosptilpriceTypeid() == 1){
                System.out.println("未缴费============");
                discharge.setDischargeStatus("未缴费");
            }
            if(discharge.getHosptilpriceTypeid() == 2){
                System.out.println("已缴费============");
                discharge.setDischargeStatus("已缴费");
            }
            discharge.setHosptilpriceTypeid(null);
        }
        startPage();
        //PageHelper.orderBy("id desc");
        List<Discharge> list = dischargeService.selectDischargeList(discharge);
/*
        List<DischargeVo> list2 = new ArrayList<>();
        for (Discharge discharg : list) {
            DischargeVo dischargeVo = new DischargeVo();
            dischargeVo.setDischargeId(discharg.getDischargeId());
            dischargeVo.setDischargeStatus(discharg.getDischargeStatus());
            dischargeVo.setDischargeTime(discharg.getDischargeTime());
            dischargeVo.setDischargePrice(discharg.getDischargePrice());
            String name = hosptilpriceTypeService.selectHosptilpriceTypeByHosptilpriceTypeid(discharg.getHosptilpriceTypeid()).getHosptilpriceTypename();
            dischargeVo.setHosptilpriceTypename(name);
            Patient patient = iPatientService.selectById(discharg.getPatientId());
            dischargeVo.setPatientName(patient.getPatientName());
            list2.add(dischargeVo);
        }*/
        return getDataTable(list);
    }

    /**
     * 导出出院费用总计列表
     */
    @RequiresPermissions("hospital:discharge:export")
    @Log(title = "出院费用总计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Discharge discharge) {
        List<Discharge> list = dischargeService.selectDischargeList(discharge);
        ExcelUtil<Discharge> util = new ExcelUtil<Discharge>(Discharge.class);
        util.exportExcel(response, list, "出院费用总计数据");
    }

    /**
     * 获取出院费用总计详细信息
     */
    @RequiresPermissions("hospital:discharge:query")
    @GetMapping(value = "/{dischargeId}")
    public AjaxResult getInfo(@PathVariable("dischargeId") Long dischargeId)
    {
        return success(dischargeService.selectDischargeByDischargeId(dischargeId));
    }

    /**
     * 新增出院费用总计
     */
    @RequiresPermissions("hospital:discharge:add")
    @Log(title = "出院费用总计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Discharge discharge)
    {
        return toAjax(dischargeService.insertDischarge(discharge));
    }

    /**
     * 修改出院费用总计
     */
    @RequiresPermissions("hospital:discharge:edit")
    @Log(title = "出院费用总计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Discharge discharge)
    {
        return toAjax(dischargeService.updateDischarge(discharge));
    }

    /**
     * 删除出院费用总计
     */
    @RequiresPermissions("hospital:discharge:remove")
    @Log(title = "出院费用总计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dischargeIds}")
    public Result remove(@PathVariable Long[] dischargeIds) {
        BigDecimal bigDecimal = new BigDecimal(0);
        for (int i = 0; i < dischargeIds.length; i++) {
            BigDecimal price = dischargeService.selectDischargeByDischargeId(dischargeIds[i]).getDischargePrice();
            bigDecimal = bigDecimal.add(price);
        }
        double value = bigDecimal.doubleValue();
        return Result.ok().data("allCost", value);
    }

    @DeleteMapping("/remove2/{dischargeIds}")
    public Result remove2(@PathVariable Long[] dischargeIds) {
        for (int i = 0; i < dischargeIds.length; i++) {
            Discharge discharge = new Discharge();
            discharge.setDischargeId(dischargeIds[i]);
            discharge.setDischargeStatus("已缴费");
            discharge.setDischargeTime(new Date());
            dischargeService.updateDischarge(discharge);
        }
        return Result.ok();
    }

}
